package com.project.web.intfc.config.druid;

import com.alibaba.druid.util.DruidPasswordCallback;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.yun.common.utils.DESUtil;

import java.util.Properties;

/**
 * Druid数据源密码加密
 *
 * @author WYY
 * @date 2017/3/14
 */
@SuppressWarnings("serial")
@ConfigurationProperties
public class DbPwCallback extends DruidPasswordCallback {
    @Override
    public void setProperties(Properties properties) {
        super.setProperties(properties);
        String pwd = properties.getProperty("password");
        if (StringUtils.isNotBlank(pwd)) {
            try {

                String password = DESUtil.decrypt(pwd);
                setPassword(password.toCharArray());
            } catch (Exception e) {
                setPassword(pwd.toCharArray());
            }
        } else {
            setPassword("".toCharArray());
        }
    }

    // 加密后的密文写到
    public static void main(String[] args) {
        String pwd = "s3s4b8#dl83$d306*#@wgo";
        System.out.println("数据库原密码:" + pwd);
        String encrypt = DESUtil.encrypt(pwd);
        System.out.println("加密后的密文:" + encrypt);
        System.out.println("解密后的密码:" + DESUtil.decrypt(encrypt));
    }
}
